package com.qf.dao.impl;

import com.qf.dao.GoodsDao;
import com.qf.entity.Goods;
import com.qf.entity.GoodsType;
import com.qf.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * @author 10202
 * @version V1.0
 * @Project xiaomi
 * @Package com.qf.dao.impl
 * @Date 2023/4/14 11:57
 */
public class GoodsDaoImpl implements GoodsDao {
    @Override
    public long getCount(String where, List<Object> params) {
        QueryRunner qr=new QueryRunner(DruidUtils.getDataSource());
        try {
            return qr.query("select count(*) from tb_goods "+where,new ScalarHandler<>(),params.toArray());
        } catch (SQLException throwables) {
            throw new RuntimeException(throwables);
        }
    }

    @Override
    public List<Goods> getGoods(int start, int ps, String where, List<Object> params) {
        params.add(start);
        params.add(ps);
        QueryRunner qr=new QueryRunner(DruidUtils.getDataSource());
        try {
            return qr.query("select * from tb_goods "+where+" limit ?,?",new BeanListHandler<>(Goods.class),params.toArray());
        } catch (SQLException throwables) {
            throw new RuntimeException(throwables);
        }
    }

    @Override
    public Goods getGoodsById(String id) {
        try {
            int intId = Integer.parseInt(id);
            QueryRunner qr=new QueryRunner(DruidUtils.getDataSource());


            return  qr.query("select g.*,t.name as tname,t.level,t.parent from tb_goods as g inner join tb_goods_type as t on g.typeid=t.id where g.id=?", new ResultSetHandler<Goods>() {
                @Override
                public Goods handle(ResultSet rs) throws SQLException {
                    Goods goods=null;
                    if (rs.next()){
                        String name = rs.getString("name");
                        Date pubdate = rs.getTimestamp("pubdate");
                        String picture = rs.getString("picture");
                        BigDecimal price = rs.getBigDecimal("price");
                        int star = rs.getInt("star");
                        int typeid = rs.getInt("typeid");
                        String intro = rs.getString("intro");
                        String tname = rs.getString("tname");
                        int level = rs.getInt("level");
                        int parent = rs.getInt("parent");
                        goods=new Goods(intId,name,pubdate,picture,price,star,intro,typeid,new GoodsType(typeid,tname,level,parent));
                    }

                    return goods;
                }
            },id);
        } catch (SQLException throwables) {
            throw new RuntimeException(throwables);
        }
    }

    @Override
    public void addGoods(Goods goods) {
        QueryRunner qr=new QueryRunner(DruidUtils.getDataSource());
        Object[] params={goods.getId(),goods.getName(),goods.getPubdate(),goods.getPicture(),goods.getPrice(),goods.getStar(),goods.getIntro(),goods.getTypeId()};
        try {
            qr.update("insert into tb_goods values (?,?,?,?,?,?,?,?)",params);
        } catch (SQLException throwables) {
            throw new RuntimeException(throwables);
        }

    }
}
